import { runApp, config, history } from 'ice'
import '@/utils/userAgent.js'
import { Loading } from '@alifd/next'

function getQueryString(name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  const urlObj = window.location
  window.urlObj = urlObj
  const r =
    urlObj.href.indexOf('#') > -1
      ? urlObj.hash.split('?')[1] && urlObj.hash.split('?')[1].match(reg)
      : urlObj.search.substr(1).match(reg)
  if (r != null) return unescape(decodeURIComponent(r[2]))
  return null
}
// window.ugmId = getQueryString('ugmName') || uuidv4().substr(1, 6)
window.ugmId = getQueryString('taskId') || ''
window.installmentId = getQueryString('installmentId') || ''
window.userType = getQueryString('userType') || 'staff'
window.ifcId = getQueryString('ifcId') || ''
console.log('window.href', window.location.href)
console.log('window.ugmId', window.ugmId)
console.log('window.installmentId', window.installmentId)
console.log('window.userType', window.userType)
console.log('window.ifcId', window.ifcId)
window.isDev = config.isDev
// window.ugmId = 'sh021'
// window.ugmId = 'cb7fac34-ee02-423c-a0b8-e8f4f0aad396'
// window.ugmId = '9bb9fa33-d4fd-4af1-aae1-fdc40b43a8e4'

window.versionNumber = 'PW_V1.0_20211116_6'
window.i = 10

const indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB

if (!indexedDB) {
  console.error('当前不支持 indexedDB 数据库')
}

const appConfig = {
  store: {
    // 可选，初始化状态
    initialStates: {
      graph: {
        data: {},
        bim: {},
        selectedIds: [],
        selectedStoreyIds: [],
        selectedRicentIds: [],
        idToEye: { ids: [], keys: {} },
        onlyToEye: { ids: [], keys: {} },
        stage: ['masonry', 'plaster'],
        projectData: {},
        projectId: window.projectId,
        ugmId: window.ugmId,
        installmentId: window.installmentId,
        userType: window.userType,
        loadingVisible: false,
        ricentDrawing: {},
        ricentDrawingBackup: {},
        font: null,
        targetId: ''
      },
      ifc: {
        ifcId: window.ifcId || ''
      }
    }
  },
  app: {
    rootId: 'BIMRemodeler'
  },
  router: {
    // type: 'browser',
    fallback: <Loading style={{ backgroundColor: '#212830' }} visible={true} shape="fusion-reactor"></Loading>
  },
  request: {
    // 可选的，全局设置 request 是否返回 response 对象，默认为 false
    throwErrIfParseFail: true,
    // withCredentials: true, // 默认请求是否带上cookie
    withFullResponse: false,
    baseURL: config.baseURL,
    headers: {},
    // 拦截器
    timeout: 1000 * 60 * 30,
    interceptors: {
      request: {
        onConfig: config => {
          // 发送请求前：可以对 RequestConfig 做一些统一处理
          return config
        },
        onError: error => {
          return Promise.reject(error)
        }
      },
      response: {
        onConfig: response => {
          // 请求成功：可以做全局的 toast 展示，或者对 response 做一些格式化
          // console.log('请求成功', response)
          return response
        },
        onError: error => {
          // 请求出错：服务端返回错误状态码
          console.log('请求出错')
          if (error) console.log(error)
          if (error.response) {
            console.log(error.response.data)
            console.log(error.response.status)
            console.log(error.response.headers)
          }
          return error
        }
      }
    }
  }
}

runApp(appConfig)

// history.push('/')
// history.push('/chart')
